--
-- Author: ping.wang
-- Date: 2019-06-25 17:53:52
--
local fileName = string.getModeName(...)
local ClassRef = class(fileName)
local table_insert = table.insert

function ClassRef:ctor(data)
	if data then
		self:init(data)
	end
end

-- int32 id = 1;
-- int32 lv = 2;
-- int32 coins = 3;
-- bool got = 4; // 是否领取过了
function ClassRef:init(msg)
	self.uuid = msg.activityUID
	self.startTime = msg.startTime
	self.endTime = msg.endTime
	self.lastLevel = msg.lastLevel
	self.lastExp = msg.lastExp
	self.nowLevel = msg.nowLevel
	self.nowExp = msg.nowExp
	self.weekGroups = msg.weekGroups or 1 --决定用那一列的数据 索引 1 2 3 4
	self.isBuyGolden = msg.isBuyGolden --是否已经购买黄金奖励
	self.sliverRewards = {}
	for k,v in ipairs(msg.sliverRewards) do
		local awardInfo = {needLevel = v.needLevel, rewards = {}, isGet = v.isGet}

		for i,j in ipairs(v.rewards) do
			local loot = UD:parseLootToBagItem(j)
			table_insert(awardInfo.rewards, loot)
		end

		table_insert(self.sliverRewards, awardInfo)
	end

	self.goldenRewards = {}
	for k,v in ipairs(msg.goldenRewards) do
		local awardInfo = {needLevel = v.needLevel, rewards = {}, isGet = v.isGet}

		for i,j in ipairs(v.rewards) do
			local loot = UD:parseLootToBagItem(j)
			table_insert(awardInfo.rewards, loot)
		end

		table_insert(self.goldenRewards, awardInfo)
	end

	self.confs = GD:getHeroTreasureByActivityId(self.uuid)
    if not self.confs then
        dump(self.uuid,"activity_battlepass is not exist")
    end

    --一些常量配置
    self.constConf = GD:getHeroTreasureConstByActivityId(self.uuid)
    if not self.constConf then
        dump(self.uuid,"activity_battlepass is not exist")
    end
	self.maxLv = 1
	for k,v in ipairs(self.confs) do
		if self.maxLv < checknumber(v.lv) then
			self.maxLv = checknumber(v.lv)
		end
	end
end

--是否解锁黄金级
function ClassRef:isGoldenUnLock()
	return self.isBuyGolden
end

--获取当前经验
function ClassRef:getCurExp()
	return self.nowExp
end

function ClassRef:getCurLv()
	return self.nowLevel
end

--获取上一次的经验以及等级
function ClassRef:getLastExp()
	return self.lastExp
end

function ClassRef:getLastLv()
	return self.lastLevel
end

function ClassRef:getSliverAwards()
	return self.sliverRewards
end

function ClassRef:getGoldenAwards()
	return self.goldenRewards
end

function ClassRef:getAwardUsingIdxStr()
	 if self.weekGroups == 1 then
		 return "silverDrop_7", "goldDrop_7"
	 elseif self.weekGroups == 2 then
		 return "silverDrop_14", "goldDrop_14"
	 elseif self.weekGroups == 3 then
		 return "silverDrop_28", "goldDrop_28"
	 else
		 --默认其他为over
		 return "silverDrop_over", "goldDrop_over"
	 end
end

--用星辉币购买黄金经验的时候查看到达某个等级所能看到的黄金奖励
function ClassRef:getEndTimeDes()
	local date = os.date("%Y/%m/%d", math.floor(self.endTime))
	return date
end

function ClassRef:getNextStartTimeDes()
    local date = os.date("%Y/%m/%d", math.floor(self.endTime))
    return date
end

function ClassRef:getShowAwardByLv(lv)
	local awards = {}
	local curLv = self:getCurLv()
	for k,v in ipairs(self.sliverRewards) do
		if v.needLevel > curLv and v.needLevel <= lv and #v.rewards > 0 then
			for i,j in ipairs(v.rewards) do
				table_insert(awards,j)
			end
		end
	end

	for k,v in ipairs(self.goldenRewards) do
		if v.needLevel > curLv and v.needLevel <= lv and #v.rewards > 0 then
			for i,j in ipairs(v.rewards) do
				table_insert(awards,j)
			end
		end
	end

	return awards
end



--根据购买的经验算出消耗以及可以升级到的等级 还有可以拿到的奖励
function ClassRef:getCanLv(addExp)

	local colName1,colName2 = self:getAwardUsingIdxStr()
	local awards = {}

	local curLv = self:getCurLv()
	local needExp = 0
	local canLv = 1
	for k,v in ipairs(self.confs) do
		local lv = checknumber(v.lv)
		local exp = checknumber(v.exp)
		local drop1 = v[colName1]
		local drop2 = v[colName2]
		if lv >= curLv then
			if lv == curLv then
				needExp = needExp + exp - self:getCurExp()
			elseif lv > curLv then
				needExp = needExp + exp
			end

			if addExp - needExp < 0 then
				canLv = lv
				break
			end

			if exp == 0 then
				canLv = lv
				break
			end
		end
	end

	awards = self:getShowAwardByLv(canLv)
	return canLv, awards
end

function ClassRef:setAwardHasGet(lv, isGolden)
	local awardInfo
	if isGolden then
		awardInfo = self:getGoldenAwards()
	else
		awardInfo = self:getSliverAwards()
	end

	for k,v in ipairs(awardInfo) do
		if v.needLevel == lv then
			v.isGet = true
			break
		end
	end
end

function ClassRef:setBuyGolden(bl)
	self.isBuyGolden = bl
end

function ClassRef:hasBuyGolden()
	return self.isBuyGolden
end


function ClassRef:getUID()
	return self.uuid
end

function ClassRef:getExpMaxByLv(lv)
	for k,v in ipairs(self.confs) do
		if checknumber(v.lv) == lv then
			return tonumber(v.exp)
		end
	end
	return nil
end

function ClassRef:setLastLvExpEqualCur()
	self.lastLevel = self.nowLevel
	self.lastExp = self.nowExp
end

--生成给List用的数据
function ClassRef:getAwardDataForList()
	local info = {}
	for i = 1, #self.goldenRewards do
		table_insert(info,{golden = self.goldenRewards[i], silver = self.sliverRewards[i], curLv = self:getCurLv(), hasBuyGolden = self:hasBuyGolden()})
	end

	return info
end

function ClassRef:getMaxLv()
	return self.maxLv
end

--计算出当前等级经验到最大等级经验需要经验和
function ClassRef:getCurLvToMaxLvNeedExp()
	local curLv = self:getCurLv()
	local curExp = self:getCurExp()
	local needExp = 0
	for k,v in ipairs(self.confs) do
		if checknumber(v.lv) == curLv then
			needExp = needExp + checknumber(v.exp) - curExp
		elseif checknumber(v.lv) > curLv then
			needExp = needExp + checknumber(v.exp)
		end
	end
	return needExp
end

function ClassRef:updateExpAndLv(exp, lv)
	self.lastLevel = self.nowLevel
	self.lastExp = self.nowExp
	self.nowLevel = lv
	self.nowExp = exp
end

--lastlv <= curLv
function ClassRef:getLvDataForAnimation(lastExp, lastLv, curExp, curLv)
	local data = {}

	if lastLv == curLv then
		for k,v in ipairs(self.confs) do
			if checknumber(v.lv) == lastLv then
				table_insert(data,{srcExp = lastExp, targetExp = curExp, lv = lastLv, maxExp = checknumber(v.exp)})
				break
			end
		end
	else
		for k,v in ipairs(self.confs) do
			if checknumber(v.lv) == lastLv then
				table_insert(data,{srcExp = lastExp, targetExp = checknumber(v.exp), lv = checknumber(v.lv), maxExp = checknumber(v.exp)})
			elseif checknumber(v.lv) > lastLv and checknumber(v.lv) < curLv then
				table_insert(data,{srcExp = 0, targetExp = checknumber(v.exp), lv = checknumber(v.lv), maxExp = checknumber(v.exp)})
			elseif checknumber(v.lv) == curLv then
				table_insert(data,{srcExp = 0, targetExp = curExp, lv = checknumber(v.lv), maxExp = checknumber(v.exp)})
				break
			end
		end
	end

	return data
end

--判断是否有红点 若有可领取的奖励 则有红点
function ClassRef:isRedPoint()
    local lv = self:getCurLv()

    for k,v in ipairs(self.sliverRewards) do
        if v.needLevel <= lv and #v.rewards > 0 and not v.isGet then
            return true
        end
    end

    if self:isGoldenUnLock() then
        for k,v in ipairs(self.goldenRewards) do
            if v.needLevel <= lv and #v.rewards > 0 and not v.isGet then
                return true
            end
        end
    end

    return false
end

--获得第一个可以领取奖励的索引id
function ClassRef:getFirstCanGetIdx()
	local lv = self:getCurLv()
	local idx = 999999
	for k,v in ipairs(self.sliverRewards) do
		if v.needLevel <= lv and #v.rewards > 0 and not v.isGet then
			idx = k
			break
		end
	end

	if self:isGoldenUnLock() then
		for k,v in ipairs(self.goldenRewards) do
			if v.needLevel <= lv and #v.rewards > 0 and not v.isGet then
				if k < idx then
					idx = k
				end
				break
			end
		end
	end

	if idx ~= 999999 then
		return idx
	end

	--如果没有可领取的，索引id应该为最大值可领取的
	for k,v in ipairs(self.goldenRewards) do
		if v.needLevel <= lv then
			idx = k
		end
	end

	return idx
end

--返回该活动的一些常量配置
function ClassRef:getConstConfig()
    return self.constConf
end

return ClassRef